package cn.ultrarisk.sprinx.mybatis.query;

/**
 * ColumnWrapper
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/5/25
 * @since V1.0
 */
public class Column {
    public static final String COLUMN_ALIAS = "%s AS %s";
    public static final String FX_ALIAS = "%s(%s) AS %s";

    public static String as(String column) {
        return String.format(COLUMN_ALIAS, column, column);
    }

    public static String as(String column, String alias) {
        return String.format(COLUMN_ALIAS, column, alias);
    }

    public static String fx(Fx fx, String column, String alias) {
        return String.format(FX_ALIAS, fx.value(), column, alias);
    }

    public static String fx(Fx fx, String column) {
        return String.format(FX_ALIAS, fx.value, column, column);
    }

    public static String count(String column, String alias) {
        return fx(Fx.COUNT, column, alias);
    }

    public static String count(String column) {
        return count(column, column);
    }

    public static String sum(String column, String alias) {
        return fx(Fx.SUM, column, alias);
    }

    public static String sum(String column) {
        return sum(column, column);
    }

    public static String avg(String column, String alias) {
        return fx(Fx.AVG, column, alias);
    }

    public static String avg(String column) {
        return avg(column, column);
    }

    public static String max(String column, String alias) {
        return fx(Fx.MAX, column, alias);
    }

    public static String max(String column) {
        return max(column, column);
    }

    public static String min(String column, String alias) {
        return fx(Fx.MIN, column, alias);
    }

    public static String min(String column) {
        return min(column, column);
    }

    public static enum Fx {
        SUM("SUM"),
        COUNT("COUNT"),
        AVG("AVG"),
        MAX("MAX"),
        MIN("MIN");

        private String value;
        Fx(String value) {
            this.value = value;
        }

        public String value() {
            return this.value;
        }
    }
}
